/** 
 * File-Name:UserDao.java
 *
 * Created on 2011-6-2 下午09:12:54
 * 
 * @author: Neo (neolimeng@gmail.com)
 * Software Engineering Institute, Peking University, China
 * 
 * Copyright (c) 2009, Peking University
 * 
 *
 */
package site.dao;

import java.sql.Connection;
import java.sql.SQLException;

import neoutil.CommonDao;
import neoutil.Page;
import site.entity.User;

/**
 * Description:
 * 
 * @author: Neo (neolimeng@gmail.com) Software Engineering Institute, Peking
 *          University, China
 * @version 1.0 2011-6-2 下午09:12:54
 */
public class UserDao extends CommonDao<User, Long> {
	public UserDao(Connection connection) {
		super(User.class, connection);
	}

	public void save(User user) throws SQLException {
		Object[] params = { user.getUuid(), user.getName(), user.getEmail(),
				user.getPassword(), user.getRid(), user.getAddress(),
				user.getTelephone(), user.getFax(), user.getContactPerson(),
				user.getDescription(), user.getEntityStatus(),
				user.getEntityDatetime(), user.getStatus(), user.getRealName(),
				user.getReal() };
		execute(SQLString.SQL_USER_INSERT, params);
	}

	public Page<User> getUnactivatedUsers(Page<User> page) {
		String sql = "SELECT * FROM user WHERE status = "
				+ User.STATUS_UNACTIVATED;
		return find(page, sql);
	}

	public void activateUser(String uuid) throws SQLException {
		String sql = "UPDATE user SET status = " + User.STATUS_ACTIVATED
				+ " WHERE uuid = ?";
		execute(sql, new Object[] { uuid });
	}

	public boolean isExist(String email) {
		String sql = "SELECT COUNT(*) FROM user WHERE email = ?";
		int count = count(sql, new Object[] { email });
		if (count > 0) {
			return true;
		} else {
			return false;
		}
	}
}
